package com.lcke.common.login;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;

public class LoginCache {
    public static final String TOKEN = "token";

    // 定义缓存超时时间，就是用户有效时间 为 5min
    private static final Long timeout = 5 * 60 * 1000L;
    private static final Cache<String, User> userCache = CacheUtil.newTimedCache(timeout);


    public static Cache<String, User> setUser(String token, User user) {
        userCache.put(token, user);
        return userCache;
    }

    public static void removeUser(String token) {
        userCache.remove(token);
    }


    public static User getUser(String token) {
        User user = new User();
        if (StrUtil.equals(SpringUtil.getProperty("lemon.token"), token)) {
            user.setUserName("柠檬");
            user.setUserId("lemon");
            return user;
        }
        User userResult = userCache.get(token);
        if (null == userResult) {
            return user;
        }
        return userResult;
    }

}
